home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_11_05
/
test_obj
/
tutils.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-01-20
|
20KB
|
517 lines
/* File: tutils.c
Copyright Norman Wilde 1993
Notes: code for utilities used in testing system
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "tutils.h"
/* ----------- buffer for strings, blocks, etc. ------------- */
#define MAXCOLLECT 10000
char collectBuf[MAXCOLLECT] = {'\0'}; /* reserve space for buffer*/
char * collectPos = collectBuf; /* points to next free space in buffer*/
void collect(char c) { /* collect char c into the buffer */
*collectPos++ = c;
assert(collectPos - collectBuf < MAXCOLLECT); // abort if buf overflows
}
void collectFirst(char c) { /* reset buf and collect c */
collectPos = collectBuf;
collect(c);
}
char * release() { /* copy the string in the buf to the
heap, terminate it, and return the copy*/
char * t = (char *) calloc(sizeof (char), collectPos - collectBuf + 1);
char * s;
char * i;
s = t;
assert ( t > 0);
for (i = collectBuf; i < collectPos; )
*t++ = *i++;
*t = '\0';
return s;
}
/* ----------- simple linked list of strings with data---------------- */
struct STRLIST * listNew(char* aStr, void * aData) {
/* Answer a new list with aStr as only element */
struct STRLIST *t = (struct STRLIST *) malloc(sizeof(struct STRLIST));
t->str = aStr;
t->data = aData;
t->next = NULL;
return t;
}
struct STRLIST * listAdd(struct STRLIST * aList,char* aStr, void * aData) {
/* Add aStr to aList */
struct STRLIST *t = (struct STRLIST *) malloc(sizeof(struct STRLIST));
t->str = aStr;
t->data = aData;
t->next = aList;
return t;
}
void * listFind(struct STRLIST * aList, char * aStr) {
/* Locate string matching aStr in aList. Answer the data ptr. if
found, NULL if not found */
struct STRLIST * v;
for(v = aList; v != NULL; v = v->next) {
if(strcmp(aStr,v->str) ==0) break;
}
if(v==NULL)
return NULL;
else
return v->data;
}
int listSize(struct STRLIST *aList) {
/* Answer the number of elements in the list */
struct STRLIST * v;
int size = 0;
for(v = aList; v != NULL; v = v->next)
size++;
return size;
}
/* --- routines to create the test driver program ------- */
#define FBUFSIZE 10000 /*size of some internal bufs */
static char Buffer[FBUFSIZE]; /*assemble strings as needed */
static char r_name[21] = {'\0'}; /*name given in the runtest
stmt currently being processed*/
static char * MainDecls = NULL; /*store on heap the declarations in
the main fcn being built */
static char * MainStmts = NULL; /*store on heap the statements in
the main fcn beint built */
static void addMainDecls(char * decls) {
/* append to MainDecls a copy of the string in decls */
char * temp;
if(MainDecls == NULL) {
MainDecls = calloc(strlen(decls) + 1, sizeof(char));
strcpy(MainDecls, decls);
return;
} else {
temp = calloc(strlen(decls) + strlen(MainDecls) + 1, sizeof(char));
strcpy(temp, MainDecls);
strcat(temp, decls);
free(MainDecls);
MainDecls = temp;
return;
}
}
static void addMainStmts(char * stmts) {
/* append to MainStmts a copy of the string in stmts */
char * temp;
if(MainStmts == NULL) {
MainStmts = calloc(strlen(stmts) + 1, sizeof(char));
strcpy(MainStmts, stmts);
return;
} else {
temp = calloc(strlen(stmts) + strlen(MainStmts) + 1, sizeof(char));
strcpy(temp, MainStmts);
strcat(temp, stmts);
free(MainStmts);
MainStmts = temp;
return;
}
}
void addBlock(char * block) {
/* add to the driver the given embedded block */
/* remove first '{' and last '}' */
char * tmpStr = calloc(strlen(block), sizeof(char));
strncat(tmpStr, block + 1, strlen(block) -2);
printf("%s\n", tmpStr);
free(tmpStr);
}
void setRunName(char * theRName) {
/* set the name of the test run to the frist 20 chars of theRName*/
strncpy(r_name, theRName, 20);
}
static char * getArgDeclStr(struct STRLIST *varList) {
/* Answer a string on the heap with a comma separated list of the
variables in varList formatted as:
<argtype> var, <argtype>var,
suitable for a function's argument declarations.
*/
char * res, * remainder, * type;
int thisVarLength, resultLength;
if(varList == NULL) return NULL;
type = getVarArgType(varList->str);
thisVarLength = strlen(varList->str) + strlen(type) + 3;
if(varList->next == NULL){
res = calloc(thisVarLength + 1, sizeof (char) );
sprintf(res, "%s %s", type, varList->str);
return res;
}
remainder = getArgDeclStr( varList->next);
resultLength = strlen(remainder) + thisVarLength + 1;
res = calloc(resultLength + 1, sizeof (char) );
sprintf(res, "%s %s,%s", type, varList->str, remainder);
free(remainder);
return res;
}
static char * genFcnString(
char * run_name,
char * fcn_name,
struct STRLIST *varList,
char* body) {
/* Answer function string (on heap) for first variable in list*/
char * declarations; /*decls for var. varList->str*/
int n; /*num. chars assembled */
char * theFunc; /*the result function string*/
char * remainingVars; /*string with vars after this*/
/* must have at least one variable in the list */
assert( varList != NULL );
declarations = getVarDeclStr(varList->str);
if(varList->next != NULL) {
remainingVars = getArgDeclStr(varList->next);
n = sprintf(Buffer,"void %s_%s(int vals[], %s){\n%s%s\n}\n",
run_name, fcn_name, remainingVars, declarations, body);
assert(n>0 && n < FBUFSIZE - 2);
free(remainingVars);
theFunc = calloc(strlen(Buffer)+1, sizeof(char));
strcpy(theFunc,Buffer);
} else {
n = sprintf(Buffer,"void %s_%s(int vals[]){\n %s%s\n}\n",
run_name, fcn_name, declarations, body);
assert(n>0 && n < FBUFSIZE - 2);
theFunc = calloc(strlen(Buffer)+1, sizeof(char));
strcpy(theFunc,Buffer);
}
return theFunc;
}
static char * genBottomFcnString(
char * run_name,
struct STRLIST *varList,
char* body) {
/* Answer function string (on heap) for the 'bottom' function
that contains the actual test code*/
int n; /*num. chars assembled */
char * theFunc; /*the result function string*/
char * remainingVars; /*string with vars after this*/
/* must have at least one variable in the list */
assert( varList != NULL );
remainingVars = getArgDeclStr(varList);
n = sprintf(Buffer,"void %s_bottom(int vals[], %s)\n%s\n",
run_name, remainingVars, body);
assert(n>0 && n < FBUFSIZE - 2);
free(remainingVars);
theFunc = calloc(strlen(Buffer)+1, sizeof(char));
strcpy(theFunc,Buffer);
return theFunc;
}
static char * getVarNameStr(struct STRLIST *varList) {
/* Answer a string on the heap with a comma separated list of the
variables in varList. If there are no variables, return
an empty string.
*/
char * res, * remainder;
int thisVarLength, resultLength;
if(varList == NULL) /* if no variables, return empty str */
return calloc(1, sizeof (char) );
thisVarLength = strlen(varList->str);
if(varList->next == NULL){
res = calloc(thisVarLength + 1, sizeof (char) );
sprintf(res, "%s", varList->str);
return res;
}
remainder = getVarNameStr( varList->next);
resultLength = strlen(remainder) + thisVarLength + 1;
res = calloc(resultLength + 1, sizeof (char) );
sprintf(res, "%s,%s", varList->str, remainder);
free(remainder);
return res;
}
static char * genArgString(struct STRLIST *varList) {
/* Answer a string (on heap) of the form:
<1stvar>[vals[n-1]], <2ndvar>, ...<nthvar>
*/
int n = list